﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ShowroomMobile.Controls
{
    public class PageIndexChangedEventArgs: EventArgs
    {
        int pageIndex;

        public int PageIndex
        {
            get { return pageIndex; }
            set { pageIndex = value; }
        }
        int pageSize;

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        public PageIndexChangedEventArgs(int index, int size)
        {
            pageIndex = index;
            pageSize = size;
        }
    }

    public delegate void PageIndexChangedEventHandler(object sender , PageIndexChangedEventArgs e);

    public partial class DataPagerCtrl : System.Web.UI.UserControl
    {
        private bool needToRender;

        public bool NeedToRender
        {
            get { return needToRender; }
            set { needToRender = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            FromIndex = 0;
            CurrentPageIndex = 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                RenderContent();
            needToRender = false;
        }

        int pageSize;
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; needToRender = true; }
        }

        const string TOTAL_PAGES = "_TotalPages";
        public int TotalPages
        {
            get { return ViewState[ClientID + TOTAL_PAGES] != null ? (int)ViewState[ClientID + TOTAL_PAGES] : 0; }
        }

        const string TOTAL_ITEMS = "_TotalItems";
        public int TotalItems
        {
            get { return ViewState[ClientID + TOTAL_ITEMS] != null ? (int)ViewState[ClientID + TOTAL_ITEMS] : 0; }
            set { ViewState[ClientID + TOTAL_ITEMS] = value; ViewState[ClientID + TOTAL_PAGES] = value / PageSize + (value % PageSize == 0 ? 0 : 1); needToRender = true; }
        }

        public int CurrentPageIndex
        {
            get { return hidSelectedPage.Value == "" ? 0 : int.Parse(hidSelectedPage.Value); }
            set { hidSelectedPage.Value = value.ToString(); }
        }

        int displayButtons;
        public int DisplayButtons
        {
            get { return displayButtons; }
            set { displayButtons = value; needToRender = true; }
        }

        protected int FromIndex
        {
            get { return ViewState[ClientID + "_FromIndex"] == null ? 0 : (int)ViewState[ClientID + "_FromIndex"]; }
            set { ViewState[ClientID + "_FromIndex"] = value; needToRender = true; }
        }

        public event PageIndexChangedEventHandler PageIndexChanged;

        private void OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            if (PageIndexChanged != null)
            {
                PageIndexChanged(this, e);
            }
        }

        protected void btnPage_Click(object sender, EventArgs e)
        {
            PageIndexChangedEventArgs arg = new PageIndexChangedEventArgs(CurrentPageIndex, PageSize);
            PageIndexChanged(this, arg);

            needToRender = true;
        }

        protected void btnPageLeft_Click(object sender, EventArgs e)
        {
            FromIndex -= DisplayButtons;
            if (FromIndex < 0)
                FromIndex = 0;
            CurrentPageIndex = FromIndex + DisplayButtons - 1;
            PageIndexChangedEventArgs arg = new PageIndexChangedEventArgs(CurrentPageIndex, PageSize);
            PageIndexChanged(this, arg);
            needToRender = true;
        }

        protected void btnPageFirst_Click(object sender, EventArgs e)
        {
            FromIndex = 0;
            CurrentPageIndex = 0;
            PageIndexChangedEventArgs arg = new PageIndexChangedEventArgs(CurrentPageIndex, PageSize);
            PageIndexChanged(this, arg);

            needToRender = true;
        }

        protected void btnPageLast_Click(object sender, EventArgs e)
        {
            CurrentPageIndex = TotalPages - 1;
            FromIndex = TotalPages - DisplayButtons;
            PageIndexChangedEventArgs arg = new PageIndexChangedEventArgs(CurrentPageIndex, PageSize);
            PageIndexChanged(this, arg);

            needToRender = true;
        }

        protected void btnPageRight_Click(object sender, EventArgs e)
        {
            if (FromIndex + DisplayButtons < TotalPages)
                FromIndex += DisplayButtons;
            CurrentPageIndex = FromIndex;
            PageIndexChangedEventArgs arg = new PageIndexChangedEventArgs(CurrentPageIndex, PageSize);
            PageIndexChanged(this, arg);

            needToRender = true;
        }

        public void RenderContent()
        {
            litPager.Text = "";
            int ToIndex = Math.Min(FromIndex + DisplayButtons - 1, TotalPages - 1);
            string htmlButton = "<a onclick='" + ClientID + "pageClick({0});' id='" + ClientID + "btn{0}' class='{1}'>{2}</a>";
            string html  = "";

            if (TotalItems <= PageSize)
                return;

            if(TotalPages > DisplayButtons)
                html += string.Format("<a onclick='" + ClientID + "pageFirst();' id='" + ClientID + "btnFirst' class='pageButton'>First</a>");

            if (ToIndex >= DisplayButtons && TotalPages > 2)
            {
                html += string.Format("<a onclick='" + ClientID + "pageLeft();' id='" + ClientID + "btnLeft' class='pageButton'>...</a>");
            }

            for (int i = FromIndex; i <= ToIndex; i++)
            {
                if (i == CurrentPageIndex)
                    html += string.Format(htmlButton, i, "currentPageButton", i + 1);
                else
                    html += string.Format(htmlButton, i, "pageButton", i + 1);
            }

            if (ToIndex < TotalPages - 1 && TotalPages > 2)
            {
                html += string.Format("<a onclick='" + ClientID + "pageRight();' id='" + ClientID + "btnRight' class='pageButton'>...</a>");
            }

            if (TotalPages > DisplayButtons)
                html += string.Format("<a onclick='" + ClientID + "pageLast();' id='" + ClientID + "btnLast' class='pageButton'>Last</a>");

            litPager.Text = html;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (needToRender)
                RenderContent();
        }
    }
}